<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('用户新增')"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">登录名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="username" name="username"
                               placeholder="登录名称" type="text">
                    </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户密码</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="password" name="password"
                               placeholder="请输入密码" type="password">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="nickname" name="nickname"
                               placeholder="请输入昵称" type="text">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <!--                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>-->
                        <input lay-skin="primary" name="depId" th:each="dep:${depList}" th:title="${dep.deptName}"
                               th:value="${dep.id}" type="checkbox">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input lay-skin="primary" name="roleId" th:each="sysRole:${tsysRoleList}"
                               th:title="${sysRole.name}"
                               th:value="${sysRole.id}" type="checkbox">
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-filter="user-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="layui-btn layui-btn-primary layui-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery', 'dtree'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var dtree = layui.dtree;
        var MODULE_PATH = rootPath + "/UserController";
        var DEPAR_PATH = rootPath + "/SysDepartmentController";
        //部门树
        // dtree.renderSelect({
        //     elem: "#selectParent",
        //     url: DEPAR_PATH+"/tree",
        //     method: 'get',
        //     selectInputName: {nodeId: "depId"},
        //     skin: "layui",
        //     dataFormat: "list",
        //     response: {treeId: "id", parentId: "parentId", title: "deptName"},
        //     selectInitVal: "1"
        // });

        //名称验证
        form.verify({
            username: function (value) {
                if (value.length < 2) {
                    return '标题至少得2个字符啊';
                }
                var msg = "";
                $.ajax({
                    url: MODULE_PATH + '/checkLoginNameUnique',
                    data: "username=" + value,
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (result) {
                        msg = result;
                    }
                });
                if (msg == 1) {
                    return "已有相同数据";
                }

            }
        });


        //保存
        form.on('submit(user-save)', function (data) {

            var roleIds = "";
            $('input:checkbox[name="roleId"]:checked').each(function () {
                roleIds += $(this).val() + ",";
            });
            roleIds = roleIds.substr(0, roleIds.length - 1);
            data.field.roleIds = roleIds;

            var depIds = "";
            $('input:checkbox[name="depId"]:checked').each(function () {
                depIds += $(this).val() + ",";
            });
            depIds = depIds.substr(0, depIds.length - 1);
            data.field.depIds = depIds;

            $.ajax({
                url: MODULE_PATH + '/add',
                data: data.field,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>